nameSpace("search");
// 搜索条件
// 0-商品 1-用户 2-标签
search.condition = {
	searchType : 0,
	searchTitle : '',
	pageNo : 1,
	pageSize : 60
};

// 查询商品信息
search.searchCommodity = function(data) {
	$("#searchInfos").html(data);
	$("#searchInfos img").lazyload({
				effect : "fadeIn"
			});
	var len = $('#search_size').val();
	if (len == search.condition.pageSize) {
		$('#search_size').remove();
		search.condition.pageNo = search.condition.pageNo + 1;
		$('#searchInfos').append('<div class="pagination" id="search_nextpage'
				+ search.condition.pageNo
				+ '"><span class="loading" >正在加载数据...</span></a></div>');
		search.next();
	}
};
// 查询用户
search.searchUser = function(data) {
	$("#searchInfos").html(data);
	var len = $('#search_size').val();
	if (len == search.condition.pageSize) {
		$('#search_size').remove();
		search.condition.pageNo = search.condition.pageNo + 1;
		$('#searchInfos').append('<div class="pagination" id="search_nextpage'
				+ search.condition.pageNo
				+ '"><span class="loading" >正在加载数据...</span></a></div>');
		search.next();
	}
};
// 查询收藏夹
search.searchFav = function(data) {
	$("#searchInfos").html(data);
	var len = $('#search_size').val();
	if (len == search.condition.pageSize) {
		$('#search_size').remove();
		search.condition.pageNo = search.condition.pageNo + 1;
		$('#searchInfos').append('<div class="pagination" id="search_nextpage'
				+ search.condition.pageNo
				+ '"><span class="loading" >正在加载数据...</span></a></div>');
		search.next();
	}
};

search.next = function() {
	$("#search_nextpage" + search.condition.pageNo).each(function() {
				$(this).one("appear", function() {
							$.ajax({
										type : "POST",
										dataType : 'html',
										data : search.condition,
										cache : false,
										url : "search_search_search.htm",
										success : function(data) {
											// 商品下一页
											if (search.condition.searchType == 0) {
												search.nextCommodityPage(data);
											}
											// 用户下一页
											if (search.condition.searchType == 1) {
												search.nextUserPage(data);
											}
											// 收藏夹下一页
											if (search.condition.searchType == 2) {
												search.nextFavPage(data);
											}
										}
									});
						});
			});

	$(window).bind("scroll", function(event) {
		$("#search_nextpage" + search.condition.pageNo).each(function() {
			if ($(window).height() + $(window).scrollTop() > $(this).offset().top) {
				$(this).trigger("appear");
			}
		});
	});

};
// 加载下一页商品信息
search.nextCommodityPage = function(data) {
	$("#searchInfos").append('<div id="nextpage' + search.condition.pageNo
			+ '" >' + data + '</div>');
	$("#search_nextpage" + search.condition.pageNo).remove();
	$('#nextpage' + search.condition.pageNo + ' img').lazyload({
				effect : "fadeIn"
			});

	var len = $('#search_size').val();
	if (len == search.condition.pageSize) {
		$('#search_size').remove();
		search.condition.pageNo = search.condition.pageNo + 1;
		$('#searchInfos').append('<div class="pagination" id="search_nextpage'
				+ search.condition.pageNo
				+ '"><span class="loading" >正在加载数据...</span></a></div>');

		search.next();
	}
};

// 加载下一页的用户
search.nextUserPage = function(data) {
	$("#searchInfos").append('<div id="nextpage' + search.condition.pageNo
			+ '" >' + data + '</div>');
	$("#search_nextpage" + search.condition.pageNo).remove();
	var len = $('#search_size').val();
	if (len == search.condition.pageSize) {
		$('#search_size').remove();
		search.condition.pageNo = search.condition.pageNo + 1;
		$('#searchInfos').append('<div class="pagination" id="search_nextpage'
				+ search.condition.pageNo
				+ '"><span class="loading" >正在加载数据...</span></a></div>');

		search.next();
	}
};
// 加载下一页的收藏夹
search.nextFavPage = function(data) {
	$("#searchInfos").append('<div id="nextpage' + search.condition.pageNo
			+ '" >' + data + '</div>');
	$("#search_nextpage" + search.condition.pageNo).remove();
	var len = $('#search_size').val();
	if (len == search.condition.pageSize) {
		$('#search_size').remove();
		search.condition.pageNo = search.condition.pageNo + 1;
		$('#searchInfos').append('<div class="pagination" id="search_nextpage'
				+ search.condition.pageNo
				+ '"><span class="loading" >正在加载数据...</span></a></div>');

		search.next();
	}

};
// 查询
search.search = function(flag) {
	$("#search_content_con").remove();
	if (search.condition.searchType == 0) {
		$("#search_content_div_id")
				.append('<div id="search_content_con"><div class="inside-content" id="searchInfos"></div></div>');
	}

	if (search.condition.searchType == 1) {
		$("#search_content_div_id")
				.append('<div id="search_content_con"><div id="searchInfos"></div></div>');

	}

	if (search.condition.searchType == 2) {
		$("#search_content_div_id")
				.append('<div id="search_content_con"><div class="block"><div class="ablist-new mtctrl" id="searchInfos"></div></div>');
	}

	search.condition.pageNo = 1;
	search.condition.searchTitle = $("#search_title").attr("value");
	$("#searchInfos")
			.html('<div class="pagination"><span class="loading" >正在加载数据...</span></a></div>');
	$.post('search_search_search.htm', search.condition, function(data) {
				// 商品的业务
				if (search.condition.searchType == 0) {
					search.searchCommodity(data);
				}
				// 用户的业务
				if (search.condition.searchType == 1) {
					search.searchUser(data);
				}
				// 收藏夹的业务
				if (search.condition.searchType == 2) {
					search.searchFav(data);
				}
			});
};

$(function() {
			search.search();
			$('#search_title').keydown(function(event) {
						if (event.keyCode == 13) {
							search.search();
						}
					});
		});

// 更改查询类型
search.changeType = function(type) {
	if (type == search.condition.searchType) {
		return;
	}
	// 更改CSS
	for (var i = 0; i < 3; i++) {
		if (type == i) {
			$("#search_type_" + i).addClass('search-selected');
		} else {
			$("#search_type_" + i).removeClass('search-selected');
		}
	}
	// 更改body样式
	if (type == 1) {
		$('body').addClass('catalog-lists');
	} else {
		$('body').removeClass('catalog-lists');
	}

	search.condition.searchType = type;
	search.search(false);
};
